# -*- coding: utf-8 -*-
import time
import json

from scrapy import Request
from scrapy_redis.spiders import RedisSpider

from amac_spider.items import MutualFundManagerItem
from amac_spider.utils.handler import get_datetime


# 公募基金管理人名录
class FundManagerSpider(RedisSpider):
    name = 'mutual_fund_manager'

    def parse(self, response):
        item = MutualFundManagerItem()
        mappings = {'company_name': 'houseName',
                    'register_addr': 'registerAddr', 'office_addr': 'officeAddr'}
        info_list = json.loads(response.text)['data']['data']['dataList']
        for info in info_list:
            for k, v in mappings.items():
                item[k] = info[v]
            item['crawl_time'] = get_datetime(time.time())
            yield item

    def make_requests_from_url(self, url):
        url = url + '&type=list'
        return Request(url=url, dont_filter=True)
